package api.business;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import common.BaseController;
import common.CommonResult;
import common.validateUser.InterfaceUsers;
import common.validateUser.ValidateUserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.InspectorInfoServices;
import services.dictionary.ItemTypeServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.ExpertBaseServices;
import services.expertInfo.ExpertCertificateServices;
import services.staff.StaffInfoServices;
import util.DateUtil;
import validator.IdCardFluentValidator;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.business.BusinessInfo;
import wt.entity.business.InspectorInfo;
import wt.entity.expertInfo.ExpertBase;
import wt.entity.expertInfo.ExpertBaseExample;
import wt.entity.expertInfo.ExpertCertificate;
import wt.staticData.ShortData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping("httpApi")
public class AddBusinessExpertsController extends BaseController {
    @Autowired
    public InterfaceUsers interfaceUsers;
    @Autowired
    ItemTypeServices itemTypeServices;
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    StaffInfoServices staffInfoServices;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    InspectorInfoServices InspectorInfoServices;


    /**
     * 添加专家
     *
     * @param uid
     * @param timeStamp
     * @param sign
     * @param businessCode
     * @param leaderIdCard
     * @param crewIdCardList
     * @param startTime
     * @param endTime
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "addBusinessExperts", produces = "application/json;charset=UTF-8")
    public CommonResult addBusinessExperts(
            String uid,
            Long timeStamp,
            String sign,
            String businessCode,
            String leaderIdCard,
            String crewIdCardList,
            @DateTimeFormat(pattern = DateUtil.DATE_PATTEN) Date startTime,
            @DateTimeFormat(pattern = DateUtil.DATE_PATTEN) Date endTime

    ) throws Exception {


        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(uid, new NotBlankFluentValidator("uid"))
                .on(timeStamp, new NotNullFluentValidator("timeStamp"))
                .on(sign, new NotBlankFluentValidator("sign"))
                .on(businessCode, new NotBlankFluentValidator("businessCode"))
                .on(leaderIdCard, new IdCardFluentValidator("leaderIdCard"))
                .on(startTime, new NotNullFluentValidator("startTime"))
                .on(endTime, new NotNullFluentValidator("endTime"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            CommonResult commonResult = new CommonResult();
            commonResult.addMessage(result);
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }


        CommonResult commonResult = ValidateUserUtil.validateUser(uid, timeStamp, sign, interfaceUsers);  //用户验证
        if (!CommonResult.ZJ001.equals(commonResult.getCode())) {
            return commonResult;
        }


        BusinessInfo businessInfo = businessInfoServices.selectByBusinessCode(businessCode);
        if (businessInfo == null) {
            commonResult.setMessage(CommonResult.ZJ005_M);
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }
        if (businessInfo.getBusinessWorkFlow() != ShortData.s0) {
            commonResult.setMessage(CommonResult.ZJ012_M);
            commonResult.setCode(CommonResult.ZJ012);
            return commonResult;
        }


        if (startTime.getTime() > endTime.getTime()) {
            commonResult.setMessage(CommonResult.ZJ005_M + "startTime>endTime");
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }


        List<String> allExpertIdCardList = new ArrayList<String>();
        if (!StringUtils.isBlank(crewIdCardList)) {
            allExpertIdCardList.addAll(Arrays.asList(crewIdCardList.trim().split(",")));
        }


        allExpertIdCardList.add(leaderIdCard);

        ExpertBaseExample expertBaseExample = new ExpertBaseExample();
        expertBaseExample.createCriteria().andIdCardIn(allExpertIdCardList);
        List<ExpertBase> expertBaseList = expertBaseServices.selectByExample(expertBaseExample);

        if (allExpertIdCardList.size() != expertBaseList.size()) {
            commonResult.setMessage(CommonResult.ZJ005_M + "专家身份号不存在");
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }

        List<InspectorInfo> inspectorInfoList = new ArrayList<InspectorInfo>();

        for (ExpertBase expertBase : expertBaseList) {
            InspectorInfo inspectorInfo = new InspectorInfo();
            inspectorInfo.setBusinessId(businessInfo.getId());
            inspectorInfo.setExpertId(expertBase.getId());
            inspectorInfo.setExpertIdCard(expertBase.getIdCard());
            inspectorInfo.setExpertName(expertBase.getName());
            inspectorInfo.setExpertPhone(expertBase.getPhone());
            inspectorInfo.setBankCard(expertBase.getBankCard());
            inspectorInfo.setBankName(expertBase.getBankName());
            inspectorInfo.setCreateTime(new Date());

            if (expertBase.getIdCard().equals(leaderIdCard)) {
                inspectorInfo.setExpertRole("leader");
            } else {
                inspectorInfo.setExpertRole("crew");
            }

            ExpertCertificate expertCertificate = expertCertificateServices.adminSelect(expertBase.getId(), businessInfo.getItemId());
            if (expertCertificate == null) {
                commonResult.setMessage(CommonResult.ZJ009_M + inspectorInfo.getExpertName());
                commonResult.setCode(CommonResult.ZJ009);
                return commonResult;
            }
            inspectorInfo.setCertificatId(expertCertificate.getId());
            inspectorInfo.setCertificateTypeId(expertCertificate.getCertificateTypeId());
            inspectorInfo.setCertificateGrade(expertCertificate.getGrade());
            inspectorInfo.setCertificateNum(expertCertificate.getNum());
            inspectorInfo.setCertificateName(expertCertificate.getName());
            inspectorInfo.setCertificateStartTime(expertCertificate.getStartTime());
            inspectorInfo.setCertificateEndTime(expertCertificate.getEndTime());
            inspectorInfo.setCertificateSrc(expertCertificate.getImgSrc());

            inspectorInfoList.add(inspectorInfo);

        }

        InspectorInfoServices.insertSelective(inspectorInfoList, businessInfo.getId(),startTime,endTime);
        commonResult.setMessage(CommonResult.ZJ001_M);
        commonResult.setCode(CommonResult.ZJ001);
        return commonResult;
    }


}
